﻿using System;

namespace TinyEngine.Core.Components
{
    public class SpawnEntityComponent : TinyComponentBase
    {
        float internalTime = 0;
        public SpawnEntityComponent(IEntity owner) : base(owner)
        {
        }

        public float Time { get; set; }
        public string TemplateName { get; set; }
        
        public override void Update(float elapsed)
        {
            if (internalTime <= 0)
                return;

            internalTime -= elapsed;
            if (internalTime <= 0)
            {
                var item = this.Owner.SceneOwner.CreateTemplateClone(this.TemplateName);
                item.Position = this.Owner.Position;

                this.Owner.SceneOwner.AddEntity(item);

            }
        }

        public override void Register()
        {
            internalTime = Time;
        }
    }
}
